package com.jimikongjian.service;

import com.jimikongjian.dao.IApplicationInfo;
import com.jimikongjian.dao.IUserInfo;
import com.jimikongjian.models.*;
import com.jimikongjian.service.base.*;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/15.
 */
@Service("applicationInfoService")
public class ApplicationInfoService implements IApplicationInfoService {


    /**
     * 用户申请通过时，发送给用户的消息
     */
    public static final String PASS = "已同意你的申请。";


    /**
     * 用户申请被拒绝时，发送给用户的消息
     */
    public  static final String REFUSE = "拒绝了你的申请。";

    /**
     * 用户申请加入空间时，发送给用户的消息
     */
    public static final String JionMessage = "您申请加入";

    /**
     * 系统id
     */
    public static final int SystemId = 1;

    /**
     * 系统昵称
     */
    public static final String SystemNick = "系统通知";


    @Autowired
    @Qualifier("responseMessageService")
    private ResponseMessageService responseMessageService;


    @Autowired
    @Qualifier("iApplicationInfoImpl")
    private IApplicationInfo iApplicationInfoImpl;


    @Autowired
    @Qualifier("userService")
    private IUserService userService;


    @Autowired
    @Qualifier("iUserInfoImpl")
    private IUserInfo userInfoImpl;


    @Autowired
    @Qualifier("roomService")
    private IRoomService roomService;


    @Autowired
    @Qualifier("roomFollowerService")
    private IRoomFollowerService roomFollowerService;


    @Autowired
    @Qualifier("notificationService")
    private INotificationService notificationService;

    /**
     * 创建一个申请
     *
     * @param applicationInfo
     * @return
     */
    @Transactional
    public ResponseMessage createApplicationInfo(ApplicationInfo applicationInfo) {
        if (applicationInfo.getApplicatorId() > 0
                && applicationInfo.getApplicatorNick() != ""
                && applicationInfo.getContent() != ""
                && applicationInfo.getRoomId() > 0
                && applicationInfo.getRoomName() != ""
                && applicationInfo.getRoomCreator() > 0
                && applicationInfo.getStatus() == ApplicationInfo.STATUS_UNREADED){
            if (userService.hasUser(applicationInfo.getApplicatorId())
                    && userService.hasUser(applicationInfo.getRoomCreator())){
                if (roomService.hasRoom(applicationInfo.getRoomId())){
                    iApplicationInfoImpl.insertApplication(applicationInfo);

                    Notification notification = new Notification();
                    notification.setNotifyId(SystemId);
                    notification.setNotifyNick(SystemNick);
                    notification.setContent(ApplicationInfoService.JionMessage +"\"" + applicationInfo.getRoomName() + "\"");
                    notification.setNotifiedId(applicationInfo.getApplicatorId());
                    notification.setNotifiedNick(applicationInfo.getApplicatorNick());
                    notificationService.createNotification(notification);
                    return responseMessageService.makeMessage(Message.SUCCESS);
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ROOM);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询用户已申请的空间信息
     *
     * @param applicatorId
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectApplicationInfoByApplicatorId(int applicatorId, int start, int length) {
        if (applicatorId > 0 && start >= 0 && length > 0){
            if (userService.hasUser(applicatorId)){
                List<ApplicationInfo> applicationInfoList = iApplicationInfoImpl.getApplicationListByApplicatorId(applicatorId, start, length);
                for (int i = 0; i < applicationInfoList.size(); i ++){
                    ApplicationInfo applicationInfo = applicationInfoList.get(i);
                    if (roomFollowerService.hasUser(applicationInfo.getApplicatorId(), applicationInfo.getRoomId())){
                        applicationInfo.setStatus(1);
                        applicationInfo.setType(1);
                    }else {
                        applicationInfo.setStatus(1);
                    }
                    iApplicationInfoImpl.updateApplication(applicationInfo);
                }
                return responseMessageService.makeMessage(applicationInfoList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询申请该用户拥有的空间的申请信息
     *
     * @param roomCreator
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectApplicationListByRoomCreator(int roomCreator, int start, int length) {
        if (roomCreator > 0 && start >= 0 && length > 0){
            if (userService.hasUser(roomCreator)){
                List<ApplicationInfo> applicationInfoList = iApplicationInfoImpl.getApplicationListByRoomCreator(roomCreator, start, length);
                for (int i = 0; i < applicationInfoList.size(); i ++){
                    ApplicationInfo applicationInfo = applicationInfoList.get(i);
                    if (roomFollowerService.hasUser(applicationInfo.getApplicatorId(), applicationInfo.getRoomId())){
                        applicationInfo.setStatus(1);
                        applicationInfo.setType(1);
                    }else {
                        applicationInfo.setStatus(1);
                    }
                    iApplicationInfoImpl.updateApplication(applicationInfo);
                }
                return responseMessageService.makeMessage(applicationInfoList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询申请该用户拥有的空间的未读的申请信息
     *
     * @param roomCreator
     * @param start
     * @param length
     * @return
     */
    public ResponseMessage selectUnreadApplicationListByRoomCreator(int roomCreator, int start, int length) {
        if (roomCreator > 0 && start >= 0 && length > 0){
            if (userService.hasUser(roomCreator)){
                List<ApplicationInfo> applicationInfoList = iApplicationInfoImpl.getUnreadApplicationListByRoomCreator(roomCreator, start, length);
                for (int i = 0; i < applicationInfoList.size(); i ++){
                    ApplicationInfo applicationInfo = applicationInfoList.get(i);
                    if (roomFollowerService.hasUser(applicationInfo.getApplicatorId(), applicationInfo.getRoomId())){
                        applicationInfo.setStatus(1);
                        applicationInfo.setType(1);
                    }else {
                        applicationInfo.setStatus(1);
                    }
                    iApplicationInfoImpl.updateApplication(applicationInfo);
                }
                return responseMessageService.makeMessage(applicationInfoList, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 查询申请该用户未读申请消息的条数
     *
     * @param roomCreator
     * @return
     */
    @Override
    public ResponseMessage selectUnreadCountByRoomCreator(int roomCreator) {
        if (roomCreator > 0 ) {
            if (userService.hasUser(roomCreator)) {
                List result = iApplicationInfoImpl.getUnreadCountByRoomCreator(roomCreator);
                return responseMessageService.makeMessage(result, Message.SUCCESS);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }

    /**
     * 更新申请信息
     *
     * @param applicationInfo
     * @return
     */
    @Transactional
    public ResponseMessage updateApplicationInfo(ApplicationInfo applicationInfo) {
        if (applicationInfo.getId() > 0
                && (applicationInfo.getStatus() == ApplicationInfo.STATUS_UNREADED
                || applicationInfo.getStatus() == ApplicationInfo.STATUS_READED)
                && (applicationInfo.getType() == ApplicationInfo.TYPE_UNKNOW
                || applicationInfo.getType() == ApplicationInfo.TYPE_REFUSE
                || applicationInfo.getType() == ApplicationInfo.TYPE_PASS)
                && applicationInfo.getApplicatorId() > 0
                && applicationInfo.getApplicatorNick() != ""
                && applicationInfo.getRoomCreator() > 0
                && applicationInfo.getRoomId() > 0
                && applicationInfo.getRoomName() != ""){
            if (userService.hasUser(applicationInfo.getApplicatorId())){
                if (roomService.hasRoom(applicationInfo.getRoomId())){
                    iApplicationInfoImpl.updateApplication(applicationInfo);
                    if (applicationInfo.getType() == ApplicationInfo.TYPE_PASS){
                        RoomFollower roomFollower = new RoomFollower();
                        roomFollower.setUserId(applicationInfo.getApplicatorId());
                        roomFollower.setRoomId(applicationInfo.getRoomId());
                        roomFollowerService.followerRoom(roomFollower);
                        /**
                         *
                         * 这里还应该创建一个消息，通知申请者，他的申请已被通过了
                         *
                         */
                        UserInfo userInfo = userInfoImpl.getUserInfoByUserId(applicationInfo.getRoomCreator());
                        /**
                         * 添加消息，提醒申请该空间的用户，他通过了
                          */
                        Notification notification1 = new Notification();
                        notification1.setNotifyId(SystemId);
                        notification1.setNotifyNick(SystemNick);
                        notification1.setContent("\"" + userInfo.getNick() + "\""+"通过了您加入"
                                    +"\""+applicationInfo.getRoomName() +"\""+ "的申请。");
                        notification1.setNotifiedId(applicationInfo.getApplicatorId());
                        notification1.setNotifiedNick(applicationInfo.getApplicatorNick());
                        notificationService.createNotification(notification1);

                        /**
                         * 添加消息，提醒用户通过了某一用户的申请
                         */
                        Notification notification2 = new Notification();
                        notification2.setNotifyId(SystemId);
                        notification2.setNotifyNick(SystemNick);
                        notification2.setContent("您通过了"+"\"" + applicationInfo.getApplicatorNick() + "\""
                                + "加入"+"\""+applicationInfo.getRoomName() +"\""+ "的申请。");
                        notification2.setNotifiedId(userInfo.getUserId());
                        notification2.setNotifiedNick(userInfo.getNick());
                        notificationService.createNotification(notification2);

                        /**
                         * 更新空间人数，将空间人数加1
                         */
                        roomService.updateAddPeoplesCount(applicationInfo.getRoomId());


                    }else if (applicationInfo.getType() == ApplicationInfo.TYPE_REFUSE){
                        /**
                         *
                         *
                         * 这里应该创建一个消息，通知申请者，他的申请被拒绝了
                         *
                         */
                        UserInfo userInfo = userInfoImpl.getUserInfoByUserId(applicationInfo.getRoomCreator());
                        /**
                         * 添加消息，提醒用户申请被拒绝了
                         */
                        Notification notification1 = new Notification();
                        notification1.setNotifyId(SystemId);
                        notification1.setNotifyNick(SystemNick);
                        notification1.setContent("\"" + userInfo.getNick() + "\""+"拒绝了您加入"
                                +"\""+applicationInfo.getRoomName() +"\""+ "的申请。");
//                                ("\"" + userInfo.getNick() + "\"" + ApplicationInfoService.PASS);
                        notification1.setNotifiedId(applicationInfo.getApplicatorId());
                        notification1.setNotifiedNick(applicationInfo.getApplicatorNick());
                        notificationService.createNotification(notification1);

                        /**
                         * 添加消息，提醒用户通过了某一用户的申请
                         */
                        Notification notification2 = new Notification();
                        notification2.setNotifyId(SystemId);
                        notification2.setNotifyNick(SystemNick);
                        notification2.setContent("您拒绝了"+"\"" + applicationInfo.getApplicatorNick() + "\""
                                + "加入"+"\""+applicationInfo.getRoomName() +"\""+ "的申请。");
                        notification2.setNotifiedId(userInfo.getUserId());
                        notification2.setNotifiedNick(userInfo.getNick());
                        notificationService.createNotification(notification2);

                    }
                }
                return responseMessageService.makeMessage(Message.NO_HAVE_ROOM);
            }
            return responseMessageService.makeMessage(Message.NO_HAVE_USER);
        }
        return responseMessageService.makeMessage(Message.PARAMS_ERROR);
    }
}
